//
//  GBKTabController.m
//  GBKHouse
//
//  Created by XMYY-19 on 2017/8/30.
//  Copyright © 2017年 XMYY-19. All rights reserved.
//

#import "GBKTabController.h"
#import "HomeController.h"
#import "RegionController.h"
#import "MyMebermerController.h"
#import "WXNavigationController.h"
#import "UIColor+GNExtension.h"
#import "Global1.h"
#import "ZDGiftCategoryViewController.h"
#import "CollectionViewController.h"
@interface GBKTabController ()
@property(nonatomic,strong)NSArray *stateArr;

@end

@implementation GBKTabController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
      [self equipmentState];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.stateArr =[[NSArray  alloc] init];
    
    UIView *bgView =[[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor =[UIColor colorWithRGBColorByString:@"#394b8b"];
    bgView.alpha = 0.5;
    [self.tabBar insertSubview:bgView atIndex:0];
    
    
    
    NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] =  [UIFont systemFontOfSize:kFONT_SIZE_FROM_PX_VALUE(20)];
    attrs[NSForegroundColorAttributeName] = [UIColor colorWithRGBColorByString:@"#181d42"];
    
    NSMutableDictionary * dataAttrs = [NSMutableDictionary dictionary];
    dataAttrs[NSFontAttributeName] =  [UIFont systemFontOfSize:kFONT_SIZE_FROM_PX_VALUE(20)];
    dataAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRGBColorByString:@"#181d42"];
    
    
    UITabBarItem * item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:dataAttrs forState:UIControlStateSelected];
    

    
    [self setupAllChildViewController];
    
    [self setupAllTabBarButton];
  
}
- (void)equipmentState
{
    
    //    if(self.state == RefreshStateLoading)
    //        return;
    //    self.state = RefreshStateLoading;
    
    NSString *refresh = @"1";
    //    if (self.refreshTime == 12) {
    //        refresh = @"2";
    //    }
    
    //  NSLog(@"____equipmentState:%d:%@", self.refreshTime, refresh);
    
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"sessionId\":\"%@\",\"refresh\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, sessionId, refresh];
    // [WebServices setShowConnectting:NO];//关闭提示
    [WebServices requestStartOrder:@"getEquipmentState" body:body target:self action:@selector(equipmentStateEnd:) errorTip:@"No Prompt"];
    
}


- (void)equipmentStateEnd:(NSDictionary *)dic
{
    //self.state = RefreshStateEnd;
    
    [WebServices setShowConnectting:NO];//关闭提示
    // NSString *result = [dic objectForKey:@"result"];
    NSNumber *result = [dic objectForKey:@"result"];
    NSArray *states = [dic objectForKey:@"states"];
    if (result.integerValue == 0) {
        [self setRoomsState:states];
    }
    //    for (NSDictionary *stateDic in states) {
    //        NSString *switchId = [stateDic objectForKey:@"switchId"];
    //        NSString *state = [stateDic objectForKey:@"state"];
    //    }
    //    NSLog(@"%@,%@", result, sessionId);
    
    //    self.refreshTime++;
    //    if (self.refreshTime > 12) {
    //        self.refreshTime = 0;
    //    }
    
}
- (void)setRoomsState:(NSArray *)states
{
    _stateArr = states;
}
// ----------------------------------------------------------------------------
// 添加所有子控制器
- (void)setupAllChildViewController
{
    [self setupOneChildViewController:[[HomeController alloc] init]];

    [self setupOneChildViewController:[[CollectionViewController alloc] init]];

   // [self setupOneChildViewController:[[MyMebermerController alloc] init]];
    [self addChildViewController:[[MyMebermerController alloc] init]];
}

// ----------------------------------------------------------------------------
// 添加一个子控制器
- (void)setupOneChildViewController:(UIViewController *)viewController
{
    // 1.包装导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    nav.navigationBar.translucent = YES;
    [nav.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    nav.navigationBar.shadowImage = [UIImage new];
    [self addChildViewController:nav];
    
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    [super viewWillAppear:animated];
//}
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//    [super viewWillDisappear:animated];
//}

// ----------------------------------------------------------------------------
// 设置所有tabBarButton
- (void)setupAllTabBarButton
{
    

    // 精华tabBarItem设置
    [self setupOneTabBarButtonVcIndex:0 titile:@"首页" imageName:@"icon_home_nor" selectedImageName:@"icon_home_pre"];
    
    // 新帖tabBarItem设置
    [self setupOneTabBarButtonVcIndex:1 titile:@"区域" imageName:@"icon_quyu_nor" selectedImageName:@"icon_quyu_pre"];
    
    // 关注tabBarItem设置
    [self setupOneTabBarButtonVcIndex:2 titile:@"个人" imageName:@"icon_person_nor" selectedImageName:@"icon_person_pre"];
}

// ----------------------------------------------------------------------------
// 设置一个tabBarButton信息
- (void)setupOneTabBarButtonVcIndex:(NSInteger)vcIndex titile:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.获取子控制器(UINavigationController),并设置标题和图片
    WXNavigationController *nav = self.childViewControllers[vcIndex];
    
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   
}
@end
